<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>栈</title>
</head>

<body>
    <script type="module">
        import { Stack } from '../structure/stack/stack.js'

        const stack = new Stack()

        stack.push('a')
        stack.push('b')
        stack.push('c')
        stack.push('d')

        console.log('当前栈元素为：', stack.items)
        console.log('栈顶出栈：', stack.pop())
        console.log('当前栈元素为：', stack.items)

        console.log('栈顶元素为：', stack.peak())
        console.log('栈是否为空：', stack.isEmpty())
        console.log('当前栈长度：', stack.size())

        // 应用: 十进制 转 二进制
        function dec2binary(num) {

            const stack = new Stack()
            // 把余数依次入栈, 然后再出栈, 实现余数倒序输出
            while (num > 0) {
                let remainder = num % 2
                num = Math.floor(num / 2)
                stack.push(remainder)
            }
            // 拼接字符串
            let binString = ""
            while (!stack.isEmpty()) {
                binString += stack.pop()
            }
            return binString
        }

        console.log('100转2进制', dec2binary(100))
        console.log('35转2进制', dec2binary(35))
    </script>
</body>

</html>